﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using MSD.WL.Site.Models;

namespace MSD.WL.Site.Controllers
{
    [Authorize]
    public class UsersController : Controller
    {
        #region 用户信息
        // GET: Users
        private EMSContext db = new EMSContext();
        public ActionResult UserInfo()
        {
            return View();
        }
        [HttpPost]
        public JsonResult getUserRecords(int offset,int limit)
        {
            var userInfo = db.User as IQueryable<User>;
            var userRecords = userInfo.Select(x => new { ID = x.ID, OperatorCode = x.OperatorCode, OperatorName = x.OperatorName, DepartmentName = x.DepartmentName }).ToList();
            var total = userRecords.Count();
            var rows = userRecords.OrderBy(x => x.ID).Skip(offset).Take(limit).ToList();
            return Json(new { total = total, rows = rows });
        }
        #endregion

        #region 修改密码
        [Authorize]
        public ActionResult passwordView()
        {
            return View();
        }
        [HttpPost]
         public JsonResult changePwd(string pwd)
        {
            string usercode = User.Identity.Name;
            var user = db.User.FirstOrDefault(x => x.OperatorCode == usercode);
            string oldPwd = user.PassWord;
            user.PassWord = pwd;
            db.SaveChanges();
            return Json("isok");
        }

        [HttpPost]
        public JsonResult getPwd()
        {
            string usercode = User.Identity.Name.Trim();
            var user = db.User.FirstOrDefault(x => x.OperatorCode == usercode);
            var data = new { oldPwd = user.PassWord, usercode = usercode };
            return Json(data);
        }
        #endregion
    }
}